package com.ay.smartpark.sdk.controller;


import com.ay.smartpark.sdk.common.base.CommonConstant;
import com.ay.smartpark.sdk.common.base.ResponseBean;
import com.ay.smartpark.sdk.common.utils.CookieUtil;
import com.qycloud.SDK.server.client.DecodeAndEncodeClient;
import com.qycloud.SDK.server.client.UserClient;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Map;

@RestController
public class LoginController {

    private static final Logger logger = LoggerFactory.getLogger(LoginController.class);

    @Resource
    private DecodeAndEncodeClient decodeAndEncodeClient;

    @Resource
    private UserClient userClient;

    @RequestMapping("/login")
    public ResponseBean login(HttpServletRequest request, HttpServletResponse response) {
        try {
            String userId = "EM";
            String password = decodeAndEncodeClient.encode3DES("em123456");
            String token = userClient.getToken();
            String ip = "127.0.0.1";
            Map<String, Object> data = userClient.login(userId, password, token, ip, false);
            String sessionId = data.get("sessionId").toString();
            CookieUtil.setValue(response, CommonConstant.PHPSESSID, sessionId);
            return ResponseBean.success("login success");
        } catch (Exception e) {
            logger.error("login error:", e);
            return ResponseBean.fail("");
        }
    }


}
